//
//  UITheme.m
//  IngCard
//
//  Created by kf on 2017/8/10.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDTheme.h"
#import "IQKeyboardManager.h"
#import "YDStyle.h"
#import "YDColor.h"
#import "YDString.h"


NSString *const YDCurrentTheme = @"YDCurrentTheme";

@implementation YDTheme
@synthesize themeTemplate = _themeTemplate;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static YDTheme *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
        instance.Notification_ThemeChanged = @"Notification_ThemeChanged";
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}

#pragma mark - public
-(void) render
{
    [self renderTabbar];
    [self renderNavigationBar];
    [self renderStatusBarStyle:YDStatusBarStyle_Light];
    [self renderKeyBoard];
    
    [STYLE renderDefault];
    [COLOR renderDefault];
    if (self.themeTemplate == YDTheme_CH)
    {
        [STRING renderCN];
    }else
    {
        [STRING renderEN];
    }
    
}


-(void) renderNavigationBar:(YDBaseViewController *) vc
                      style:(YDNavigationBarStyle) style
{
    if (vc && vc.navigationController)
    {
        UIColor *backgroundColor = UIColorClear;
        UIColor *textColor = UIColorMake(255, 255, 255);
        switch (style) {
            case YDNavigationStyle_Red:
                backgroundColor = UIColorMake(228, 94, 74);
                break;
            case YDNavigationStyle_Gray:
                backgroundColor = UIColorMake(225, 222, 222);
                textColor = UIColorMake(60, 44, 44);
                break;
            case YDNavigationStyle_Blue:
                backgroundColor = UIColorMake(21, 26, 65);
                break;
            case YDNavigationStyle_Green:
                backgroundColor = UIColorMake(104, 151, 126);
                break;
            case YDNavigationStyle_Clear:
                backgroundColor = UIColorWhite;//  UIColorMake(225, 222, 222);
                textColor = UIColorMake(0, 0, 0);
                break;
            default:
                break;
        }
    
        vc.titleView.titleLabel.font = UILightFontMake(20);
        vc.titleView.titleLabel.textColor = textColor;
        [vc.navigationController.navigationBar setBarTintColor:backgroundColor];
        [vc.navigationController setNavigationBarHidden:NO animated:YES];
        
        
        
        
//        UIBarButtonItem *readItem = [QMUINavigationButton barButtonItemWithType:QMUINavigationButtonTypeNormal
//                                                                          title:@"读"
//                                                                       position:QMUINavigationButtonPositionRight
//                                                                         target:self
//                                                                         action:@selector(read)];
//        
//        UIBarButtonItem *writeItem = [QMUINavigationButton barButtonItemWithType:QMUINavigationButtonTypeNormal
//                                                                           title:@"写"
//                                                                        position:QMUINavigationButtonPositionNone
//                                                                          target:self
//                                                                          action:@selector(write)];
//        self.navigationItem.rightBarButtonItems = @[readItem, writeItem];
//        
    }
}


- (void)renderStatusBarStyle:(YDStatusBatStyle) style
{
    switch (style) {
        case YDStatusBarStyle_Dark:
            if ([[UIApplication sharedApplication] statusBarStyle] >= UIStatusBarStyleLightContent) {
                [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
            }
            break;
        case YDStatusBarStyle_Light:
            if ([[UIApplication sharedApplication] statusBarStyle] < UIStatusBarStyleLightContent) {
                [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            }
            break;
        default:
            break;
    }
}

-(void) renderKeyBoard
{
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
//    keyboardManager.shouldResignOnTouchOutside = YES;
//    keyboardManager.keyboardDistanceFromTextField = 200;
//    keyboardManager.enableAutoToolbar = YES;
////    keyboardManager.toolbarDoneBarButtonItemText = @"完成";
////    keyboardManager.toolbarDoneBarButtonItemImage = [UIImage imageNamed:@"imageName"];
////    keyboardManager.toolbarDoneBarButtonItemImage.toolbarTintColor = [UIColor brownColor];
//    keyboardManager.keyboardDistanceFromTextField = 0.0f;
//    keyboardManager.preventShowingBottomBlankSpace = NO;
    
    
    keyboardManager.enable = YES; // 控制整个功能是否启用。
    keyboardManager.shouldResignOnTouchOutside =YES; // 控制点击背景是否收起键盘
    keyboardManager.shouldToolbarUsesTextFieldTintColor =YES; // 控制键盘上的工具条文字颜色是否用户自定义
    keyboardManager.enableAutoToolbar =NO; // 控制是否显示键盘上的工具条
    keyboardManager.toolbarManageBehaviour =IQAutoToolbarByTag; // 最新版的设置键盘的returnKey的关键字 ,可以点击键盘上的next键，自动跳转到下一个输入框，最后一个输入框点击完成，自动收起键盘。
    keyboardManager.placeholderFont = UIFontMake(18);//[UIFontboldSystemFontOfSize:18];
//    在需要使用的界面设置
//    IQKeyboardReturnKeyHandler *retuenKeyHandler = [[IQKeyboardReturnKeyHandleralloc]initWithViewController:self];
//    retuenKeyHandler.lastTextFieldReturnKeyType =UIReturnKeyDone; // 设置最后一个输入框的ReturnKey关键字
}
#pragma mark - private
- (void) renderTabbar
{
    UITabBar *tabbar = [UITabBar appearance];
//    tabbar.barTintColor = UIColorClear;
    tabbar.unselectedItemTintColor = UIColorBlack;
    tabbar.backgroundImage = [[UIImage qmui_imageWithColor:UIColorWhite] qmui_imageWithAlpha:1];
//    tabbar.selectionIndicatorImage = nil;
    tabbar.shadowImage = nil;
    
//    tabbar.itemWidth = SCREEN_WIDTH/4;
//    tabbar.itemSpacing = 0;
//    tabbar.barStyle = UIBarStyleDefault;
//    tabbar.itemPositioning = UITabBarItemPositioningCentered;
    
    UITabBarItem *tabbarItem = [UITabBarItem appearance];
    [tabbarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorMake(248, 107, 107)}
                              forState:UIControlStateSelected];
    [tabbarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColorBlack}
                              forState:UIControlStateNormal];
    
    [tabbarItem setTitlePositionAdjustment:UIOffsetMake(0, -8)];
    [tabbarItem setTitleTextAttributes:@{NSFontAttributeName:UIFontLightMake(14)} forState:UIControlStateNormal];
    [tabbarItem setTitleTextAttributes:@{NSFontAttributeName:UIFontLightMake(14)} forState:UIControlStateSelected];
}

- (void) renderNavigationBar
{
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    navigationBarAppearance.shadowImage = [UIImage new];
    
    UIFont *navigationBarTitleFont = UIFontLightMake(20);
    if (navigationBarTitleFont ) {
        NSMutableDictionary<NSString *, id> *titleTextAttributes = [[NSMutableDictionary alloc] init];
        if (navigationBarTitleFont) {
            [titleTextAttributes setValue:navigationBarTitleFont forKey:NSFontAttributeName];
        }
        navigationBarAppearance.titleTextAttributes = titleTextAttributes;
    }
    
    [UINavigationBar appearance].backIndicatorTransitionMaskImage = UIImageMake(@"navigationBack");
    
    [UINavigationBar appearance].backIndicatorImage = UIImageMake(@"navigationBack");
}


#pragma mark - Private

-(void) setThemeTemplate:(YDThemeTemplate)themeTemplate
{
    if (_themeTemplate != themeTemplate)
    {
        _themeTemplate = themeTemplate;
        if (themeTemplate == YDTheme_CH)
        {
            [STRING renderCN];
        }
        else
        {
            [STRING renderEN];
        }
        dispatch_async_main(^{
            [[NSNotificationCenter defaultCenter] postNotificationName:THEME.Notification_ThemeChanged object:nil];
        })
        [[NSUserDefaults standardUserDefaults] setObject:@(themeTemplate) forKey:YDCurrentTheme];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }
}

-(YDThemeTemplate) themeTemplate
{
    NSInteger template = [[[NSUserDefaults standardUserDefaults] objectForKey:YDCurrentTheme] integerValue];
    if (template <= 0)
    {
        template = YDTheme_CH;
    }
    return (YDThemeTemplate)template;
}
@end















